<?php
/**
 -----------------------------
 *  功能:管理员模型文件
 */
namespace Home\Model;
use Think\Model;

class IndexModel extends Model{
	public function add(){
		$datas = $_POST;
		$M=M('admin');
		if($M->where(array('username'=>$datas['username']))->count()==0){
			if(empty($datas['username'])){
				return array('status'=>5,'info'=>"用户名不能为空！");
			}else{
				if(empty($datas['password'])){
					return array('status'=>1,'info'=>"密码不能为空！");
				}else{
					if($datas['password']!=$datas['password1']){
						return array('status'=>2,'info'=>"两次密码不一致！");
					}else{
						$data['username']=$datas['username'];
						$data['password']=MD5($datas['password']);
						$data['email']=$datas['email'];
						$data['time']=time();
						$data['times']=date("Y-m-d H:i:s",time());
						$add=$M->add($data);
						if(empty($add)){
							return array('status'=>3,'info'=>"添加失败!");
						}else{
							return array('status'=>0,'info'=>"添加成功！");
						}
					}
				}
			}
		}else{
			return array('status'=>4,'info'=>"用户名已存在！");
		}
	}
	
	public function update(){
		$datas = $_POST;
		$M=M('admin');
		if(empty($datas['password'])){
			return array('status'=>3,'info'=>"密码不能为空！");
		}else{
			if($datas['password']!=$datas['password1']){
				return array('status'=>2,'info'=>"两次密码不一致！");
			}else{
				$data['password']=MD5($datas['password']);
				$data['time']=time();
				$data['times']=date("Y-m-d H:i:s",time());
				$update=$M->where(array('uid'=>$_GET['uid']))->save($data);
				if(empty($update)){
					return array('status'=>1,'info'=>"修改失败!");
				}else{
					return array('status'=>0,'info'=>"修改成功!");
				}
			}
		}
	}
}